#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_data_sync.sh
##  Functions : 数据同步
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
##  2.0      2018-11-11  薛周毅           1.根据参数【V_PARALLEL_SYNC_NO】并发导出和导入
##                                        2.并发后会检查每个进程的状态
###############################################################################
:<<!
   错误码： 1  参数格式不合法
            2  查询同步配置信息失败
            3  子进程运行失败
!
. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo "[ERROR]How to use this shell script!"
    echo "[ERROR]${V_SHELL_HOME}/edw_data_sync.sh H2M"
}

# ensure the basic variavle of connection is not null
if [[ -z ${1} ]];then
    USAGE
    exit 1
else
    V_MODE=${1}
fi

V_DATA_DATE=$(grep "#V_DATA_DATE#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
V_FREQUENCY=$(grep "#V_FREQUENCY#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_DATA_DATE}/${V_SCRIPT_NAME}_${V_FREQUENCY}.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output

# Check table
V_QUERY="SELECT COALESCE(COUNT(1),0) FROM ETL.EDW_DATA_SYNC WHERE SYNC_TYPE='${V_MODE}' AND IF_VALID='Y'"
V_TAB_CNT=$(${V_RUN_DB} -q "${V_QUERY}") || exit 2
if [[ ${V_TAB_CNT} -eq 0 ]];then
    LOGGER "WARNING" "No tables to sync..."
    END
    exit 0
else
    LOGGER "INFO" "Has ${V_TAB_CNT} tables to sync."
    echo "TAB_CNT=${V_TAB_CNT}" >> ${SYNC_SPEED_FILE}
fi

BEGIN
LOGGER "INFO" "Today batch date is ${V_DATA_DATE}."

    
# Parallel run procedure
LOGGER "INFO" "[------------------------Parallel call exporter------------------------]"
for ((i=0; i<${V_PARALLEL_SYNC_NO}; i++));do
    sh ${V_EXPT_DATA} ${V_MODE} ${i} &
    V_EXP_PID[i]=$(echo ${!})
    LOGGER "INFO" "Start to call[${V_EXP_PID[i]}]:${V_EXPT_DATA} ${V_MODE} ${i}"
done

LOGGER "INFO" "Starting to check exporter data..."
for ((i=0; i<${#V_EXP_PID[@]}; i++));do
    # Get the return code
    wait ${V_EXP_PID[i]}
    if [[ $? -eq 0 ]];then
        LOGGER "INFO" "SEQ=${i},PID=${V_EXP_PID[i]} 同步完成."
    else
        x=fail
        LOGGER "ERROR" "[SEQ=${i},PID=${V_EXP_PID[i]}]:同步数据失败."
        LOGGER "ERROR" "日志路径${V_SHELL_LOGS}/${V_DATA_DATE}/edw_data_exporter_${V_FREQUENCY}_${i}.log."
    fi
done

rm ${SYNC_SPEED_FILE}
# Check all
if [[ ${x} == "fail" ]];then
    exit 3
fi

END
exit 0
